// components/Rename/rename.js
import { request } from '../../../utils/util'
const { $Message } = require('../../../dist/base/index');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    disable: null,
    num: null,
    type: "",
    classifyType: "",
    startValue: "",
    project: "",
    typeValue: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const num = options.num
    const type = options.type
    const classifyType = options.classifyType
    let name = options.title
    let typeValue = ""
    const project = options.project || ""

    if (type === "folder" || type === "cfolder") {
      name = name
    } else {
      typeValue = "." + type
      name = name.split(typeValue)[0]
    }
    this.setData({
      ...this.data,
      num: num,
      type: type,
      classifyType: classifyType,
      value: name,
      startValue: name,
      project: project,
      typeValue: typeValue
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 键盘输入事件
   */
  input: function (e) {
    this.setData({
      ...this.data,
      value: e.detail.value
    })
    if (this.data.value.length >= 1) {

      this.setData({
        ...this.data,
        disable: false
      })
    } else {

      this.setData({
        ...this.data,
        disable: true
      })
    }
  },
  /**
   * 确认按钮
   */
  sure() {
    if (this.data.value === this.data.startValue) { //这步处理为了值不变不请求服务器
      wx.navigateBack();
    } else {
      if (this.data.type === "folder" || this.data.type === "cfolder") {
        let param = {};
        if (this.data.project !== "0" || !this.data.project) {
          param = {
            type: this.data.classifyType,
            title: this.data.value,
            project: this.data.project
          }
        } else {
          param = {
            type: this.data.classifyType,
            title: this.data.value
          }
        }
        request("PATCH", `/api/service/catalog/${this.data.num}/`, param)
          .then(res => {
            if (res.statusCode === 200) {
              const showMsg = setTimeout(() => {
                $Message({
                  content: '修改成功',
                  type: 'success'
                });
                clearTimeout(showMsg)
              }, 500)
              wx.navigateBack()
            }
          })
      } else {
        let param = {}
        if (this.data.project !== "0" || !this.data.project) {
          param = {
            title: this.data.value + this.data.typeValue,
            project: this.data.project
          }
        } else {
          param = {
            title: this.data.value + this.data.typeValue
          }
        }

        request("PATCH", `/api/service/docinfo/${this.data.num}/`, param)
          .then(res => {
            if (res.statusCode === 200) {

              const showMsg = setTimeout(() => {
                $Message({
                  content: '修改成功',
                  type: 'success'
                });
                clearTimeout(showMsg)
              }, 500)
              wx.navigateBack()
            }
          })
      }
    }


  }
})